package com.csit.dao.impl;

import java.util.Date;

import org.hibernate.Criteria;
import org.hibernate.criterion.CriteriaSpecification;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.csit.bean.Pager;
import com.csit.dao.CardChargeDao;
import com.csit.entity.CardCharge;

@Repository
public class CardChargeDaoImpl extends BaseDaoImpl<CardCharge, Integer> implements CardChargeDao {

	public Pager query(Pager pager, CardCharge cardCharge,Date beginDate,Date endDate) {
		Criteria criteria = getSession().createCriteria(CardCharge.class);
		
		criteria.createAlias("operate", "operate", CriteriaSpecification.LEFT_JOIN);
		criteria.createAlias("customerCard", "customerCard", CriteriaSpecification.LEFT_JOIN);
		
		if(cardCharge.getCustomerCard()!=null){
			criteria.add(Restrictions.eq("customerCard", cardCharge.getCustomerCard()));
		}
		
		if(beginDate!=null){
			criteria.add(Restrictions.ge("chargeDate", beginDate));
		}
		if(endDate!=null){
			criteria.add(Restrictions.le("chargeDate", endDate));
		}
		Long totalCount = (Long) criteria.setProjection(Projections.rowCount()).uniqueResult();

		criteria.setProjection(null);
		criteria.setResultTransformer(CriteriaSpecification.ROOT_ENTITY);
		criteria.setFirstResult(pager.getBeginCount());
		criteria.setMaxResults(pager.getPageSize());
		criteria.addOrder(Order.asc("customerCard.customer"));
		
		pager.setTotalCount(totalCount);
		pager.setList(criteria.list());
		
		return pager;
	}

}
